package com.yaozheng.vocationaltraining.activity;

import android.content.Intent;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.dialog.interfaces.BaseDialogEventProcess;
import com.yaozheng.vocationaltraining.iview.IHomeSignTaskView;
import com.yaozheng.vocationaltraining.service.HomeSignTaskService;
import com.yaozheng.vocationaltraining.service.impl.HomeSignTaskServiceImpl;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.HomeSignTaskView;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * 签到任务activity
 */
@EActivity(R.layout.activity_home_sign_task)
public class HomeSignTaskActivity extends BaseActivity implements IHomeSignTaskView, BaseDialogEventProcess {

    @ViewById
    HomeSignTaskView homeSignTaskView;


    @Bean(HomeSignTaskServiceImpl.class)
    HomeSignTaskService homeSignTaskService;

    @AfterViews
    public void initView() {
        homeSignTaskService.init(this);
        homeSignTaskView.setBaseDialog(this);
        homeSignTaskView.setiHomeSignTaskView(this);
        homeSignTaskService.signRecording();
    }

    @UiThread
    @Override
    public void executeSignSuccess(JSONObject jsonObject) {
        cancelOperatingProgressDialog();
        JSONObject dataJsonObject = TypeUtils.getJsonObject(jsonObject, "data");
        homeSignTaskService.signRecording();
//        alertMessage("签到成功");
        int common_award = TypeUtils.getJsonInteger(dataJsonObject, "common_award");
        int special_award = TypeUtils.getJsonInteger(dataJsonObject, "special_award", 0);

        StringBuffer messageContent = new StringBuffer();
//        if (special_award > 0) {
//            messageContent.append("领取宝箱成功/n");
//        } else {
//            messageContent.append("签到成功/n");
//        }
        messageContent.append("恭喜获得奖励积分：");
        messageContent.append((common_award + special_award));
        showCancelAskDialog(messageContent.toString(), "恭喜！签到成功", null);
    }

    @UiThread
    @Override
    public void executeSignError(String error) {
        cancelOperatingProgressDialog();
        alertMessage(error);

    }

    @UiThread
    @Override
    public void signRecordingSuccess(JSONObject jsonObject) {
        homeSignTaskView.loadData(TypeUtils.getJsonObject(
                jsonObject, "data"));
    }

    @UiThread
    @Override
    public void signRecordingError(String error) {
        cancelOperatingProgressDialog();
        alertMessage(error);
    }

    @Override
    public boolean isResponseSignResult() {
        return isResponseResult();
    }

    @Override
    public void executeSign(String day) {
        showOperatingProgressDialog("签到中...");
        homeSignTaskService.executeSign(day);
    }

    @Override
    public void responseSuccess(JSONObject jsonObject) {

    }

    @Override
    public void responseError(String error) {

    }

    /**
     * 关闭对话框
     */
    public void cancelDialog() {
        this.backClick();
    }


    @Override
    public void backClick() {
        Intent intent = new Intent();
        intent.setClassName(this, Constants.HOME_ANSWER_TASK_ACTIVITY_CLASS_NAME);
        startActivity(intent);
        super.backClick();
    }

    /**
     * 补签处理
     */
    public void retroactiveOperating() {
        homeSignTaskView.retroactiveOperating();
    }
}
